﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using CustomControlDatabaseInterface.Utilities;

namespace CustomControlDatabaseInterface.ViewModel
{
    public class LoginViewModel : WorkspaceViewModel, IDataErrorInfo
    {

        #region Fields
        private String _password;
        private String _userName;
        private String _server;
        private String _database;
        private Boolean _configureDatabaseConnectionVisible;
        private String password;

        private String userName;

        private String server;

        private String database;

        #endregion

        #region Properties

        public String Password
        {
            get { return _password; }
            set { _password = value; OnPropertyChanged("Password"); }
        }

        public String UserName
        {
            get { return userName; }
            set
            {
                userName = value;
                OnPropertyChanged("UserName");
            }
        }

        public String Server
        {
            get
            {
                return server;
            }
            set
            {
                server = value;
                OnPropertyChanged("Server");
            }
        }

        public String Database
        {
            get
            {
                return database;
            }

            set
            {
                database = value;
                OnPropertyChanged("Database");
            }
        }

        private Boolean configureConnectionVisible;

        public Boolean ConfigureConnectionVisible {
            get {
                return configureConnectionVisible;
            }
            set {
                configureConnectionVisible = value; 
                OnPropertyChanged("ConfigureConnectionVisible");
            }
        }

        public Boolean LoginVisible {
            get {
                return !configureConnectionVisible;
            }
            set {
                configureConnectionVisible = !value;
                OnPropertyChanged("LoginVisible");
            }
        }

        public Boolean LoginSuccessful { get; set; }

        public ICommand LoginCommand { get; set; }

        public ICommand ConfigureConnectionCommand { get; set; }


        #endregion

        #region Constructor

        public LoginViewModel()
        {
            DatabaseUtil.SetServerDatabase("localhost", "customcontrolsdatabase"); 
            LoginCommand = new RelayCommand(param => Login(param));
            ConfigureConnectionCommand = new RelayCommand(param => ConfigureConnection());
            LoginSuccessful = true;
            ConfigureConnectionVisible = false;

        }

        #endregion

        public string Error
        {
            get { throw new NotImplementedException(); }
        }
        
        public string this[string columnName]
        {
            get
            {
                string result = null;
                if (columnName == "UserName" && LoginSuccessful == false)
                {
                    result = "Invalid Username";

                }
                if (columnName == "Password" && LoginSuccessful == false)
                {
                    result = "Invalid Password";
                }
                return result;
            }
        }

        public void ConfigureConnection() {
            if (!ConfigureConnectionVisible) {
                ConfigureConnectionVisible = true;
                LoginVisible = false;
            }
            else {
                ConfigureConnectionVisible = false;
                LoginVisible = true;
                if(server != null)
                    DatabaseUtil.SetServerDatabase(server, database);
            }
        }

        public void Login(object param)
        {
            if (DatabaseUtil.TryOpenConnection(UserName, Password))
            {
                LoginSuccessful = true;
                OnPropertyChanged("UserName");
                OnPropertyChanged("Password");
                DatabaseUtil.SetConnection(UserName, Password);
                OnLoginSuccessful(); 
            }
            else
            {
                LoginSuccessful = false;
                OnPropertyChanged("UserName");
                OnPropertyChanged("Password");
            }

        }

        public event EventHandler LoginEvent;

        public void OnLoginSuccessful()
        {
            if (LoginEvent != null)
            {
                this.LoginEvent(this, EventArgs.Empty);
            }
        }


        
    }
}
